import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class mainconnect {
	
	private static Connection con = null;
	private static String user = "root";
	private static String pass = "root";
	private static String database = "backupnrestore";
	private static String driverName = "com.mysql.jdbc.Driver";
	private static String serverName = "localhost:3306/";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws ClassNotFoundException, SQLException{
		// TODO Auto-generated method stub
		String query = "Select * FROM userinformation";			
//		String sqlinsert = "insert into user(pass,name) values('726','johnson')";
//		executeUpdate(sqlinsert);
//		String sqlupdate = "UPDATE user SET name = 'Tina Tran thi thanh' where id = '1'";
//		executeUpdate(sqlupdate);			
		ResultSet rst = executeQuery(query);
			while (rst.next()) {
				System.out.println("id is: "+rst.getString("userid")+"--"+"name is: "+rst.getString("username"));
			} //end while
			con.close();

	}
	
	public static ResultSet executeQuery(String sql)throws ClassNotFoundException, SQLException{
		
		Class.forName(driverName);
		con = DriverManager.getConnection ("jdbc:mysql://"+serverName+database,user,pass);
		Statement stm = con.createStatement();
		ResultSet rs = stm.executeQuery(sql);
		return rs;
	}
	public static int executeUpdate(String sql) throws ClassNotFoundException, SQLException{
		
		int result = 0;
		Class.forName(driverName);
		con = DriverManager.getConnection ("jdbc:mysql://"+serverName+database,user,pass);
		Statement stm = con.createStatement();
		result = stm.executeUpdate(sql);
		con.close();
		return result;
	}

}
